/**
 *
 * Copyright 2010 TeleNav, Inc. All rights reserved.
 * EntryView.java
 *
 */
package com.telenav.module.home;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.telenav.app.R;
import com.telenav.mvc.AbstractBaseView;
import com.telenav.ui.AndroidDialog;
import com.telenav.ui.AndroidView;
import com.telenav.ui.CommandEvent;
import com.telenav.ui.UiEvent;
import com.telenav.ui.widget.ActionBar;
import com.telenav.ui.widget.ActionBar.ActionOnClickListener;
import com.telenav.ui.widget.ActionBar.IntentAction;

class HomeViewTouch extends AbstractBaseView implements IHomeConstants
{
	private Context context;
	private ArrayList<View> pageViews;
	private ViewPager viewPager;
	private ImageView imageView;
	private TextView pagesText;
	private int currrentItem;
	
	private int[] welPictures = { R.drawable.yy_profile_bg_1,
			R.drawable.yy_profile_bg_2, R.drawable.yy_profile_bg_3,
			R.drawable.yy_profile_bg_4, R.drawable.yy_profile_bg_5 };
	
    public HomeViewTouch(Context context)
    {
    	this.context = context;
    }

	@Override
	protected AndroidDialog createDialogDelegate(int state) {
    	Dialog dialog = null;
        switch (state)
        {
            case STATE_LOGIN:
            {
            	dialog = new AlertDialog.Builder(context)
            	.setTitle("Login...")
            	.create();
                break;
            }
            default:
                break;
        }
        
        return new AndroidDialog(context, dialog);
	}

	@Override
	protected boolean updateDialog(int state, AndroidDialog dialog) {
		
		return false;
	}

	@Override
	protected AndroidView createViewDelegate(int state) {
        switch(state)
        {
            case STATE_INIT:
            {
                return createHomeView(state);
            }
            case STATE_USERINFO:
            {
            	return createUserInfoView(state);
            }
            case STATE_SETTING:
            {
            	return createSetting(state);
            }
            case STATE_USER:
            {
            	return createUser(state);
            }
        }
        return null;
	}
	
	@Override
	protected boolean updateViewDelegate(int state, AndroidView view)
	{
        switch(state)
        {
            case STATE_ACTIVITY:
            {
                return true;
            }
        }
        return false;
	}
	
	private AndroidView createHomeView(int state)
	{
		final View fullView = View.inflate(context, R.layout.yy_home, null);
		
		pageViews = new ArrayList<View>();
		for (int i = 0; i < welPictures.length; i++) {
			imageView = new ImageView(context);
			imageView.setLayoutParams(new LayoutParams(10, 10));
			imageView.setPadding(10, 0, 10, 0);
			imageView.setBackgroundResource(welPictures[i]);
			imageView.setOnClickListener(new ImagePageClickListener());
			pageViews.add(imageView);
		}
		
		viewPager = (ViewPager) fullView.findViewById(R.id.main_image_pager);
		viewPager.setAdapter(new ImagePageAdapter());
		viewPager.setOnPageChangeListener(new ImagePageChangeListener());
		
		pagesText = (TextView) fullView.findViewById(R.id.main_pages_text);
		pagesText.setText((currrentItem + 1) + "/" + pageViews.size());
		
		ActionBar actionBar = (ActionBar) fullView.findViewById(R.id.actionbar);
		actionBar.setTitle("精彩推荐");
		actionBar.addRightAction(new IntentAction(
				R.drawable.yy_action_bar_search, new ActionOnClickListener() {
					public void onClick() {
						CommandEvent commandEvent = new CommandEvent(CMD_GOTO_ACTIVITYLIST);
						UiEvent event = new UiEvent(UiEvent.TYPE_COMMAND_EVENT);
						event.setCommandEvent(commandEvent);
						HomeViewTouch.this.handleUiEvent(event);
					}
				}));
		
		RelativeLayout nearLayout = (RelativeLayout) fullView.findViewById(R.id.main_near_activities);
		nearLayout.setOnClickListener(gotoActivityList);
		
		return new AndroidView(fullView);
	}
	
	OnClickListener gotoActivityList = new OnClickListener(){
		@Override
		public void onClick(View arg0) {
			
			CommandEvent commandEvent = new CommandEvent(CMD_GOTO_ACTIVITYLIST);
			UiEvent event = new UiEvent(UiEvent.TYPE_COMMAND_EVENT);
			event.setCommandEvent(commandEvent);
			HomeViewTouch.this.handleUiEvent(event);
		}
	};
	
	OnClickListener gotoHome = new OnClickListener(){
		@Override
		public void onClick(View arg0) {
			CommandEvent commandEvent = new CommandEvent(CMD_GOTO_HOME);
			UiEvent event = new UiEvent(UiEvent.TYPE_COMMAND_EVENT);
			event.setCommandEvent(commandEvent);
			HomeViewTouch.this.handleUiEvent(event);
		}
	};
	
	OnClickListener gotoUserInfo = new OnClickListener(){
		@Override
		public void onClick(View arg0) {
			CommandEvent commandEvent = new CommandEvent(CMD_GOTO_USERINFO);
			UiEvent event = new UiEvent(UiEvent.TYPE_COMMAND_EVENT);
			event.setCommandEvent(commandEvent);
			HomeViewTouch.this.handleUiEvent(event);
		}
	};
	
	OnClickListener gotoSetting = new OnClickListener(){
		@Override
		public void onClick(View arg0) {
			CommandEvent commandEvent = new CommandEvent(CMD_GOTO_SETTING);
			UiEvent event = new UiEvent(UiEvent.TYPE_COMMAND_EVENT);
			event.setCommandEvent(commandEvent);
			HomeViewTouch.this.handleUiEvent(event);
		}
	};
	
	private AndroidView createUserInfoView(int state)
	{
        View fullView = View.inflate(context, R.layout.yy_login, null);
        
		ActionBar actionBar = (ActionBar) fullView.findViewById(R.id.actionbar);
		actionBar.setTitle("登陆");
        
        TextView okBtn = (TextView)fullView.findViewById(R.id.btn_login);
        TextView registerBtn = (TextView)fullView.findViewById(R.id.btn_login_register);
        final TextView name = (TextView)fullView.findViewById(R.id.input_username); 
        final TextView pwd = (TextView)fullView.findViewById(R.id.input_pwd);
		
        okBtn.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View arg0) 
			{
				model.put(KEY_S_USERNAME, name.getText().toString());
				model.put(KEY_S_PWD, pwd.getText().toString());
				
				CommandEvent commandEvent = new CommandEvent(CMD_LOGIN);
				UiEvent event = new UiEvent(UiEvent.TYPE_COMMAND_EVENT);
				event.setCommandEvent(commandEvent);
				HomeViewTouch.this.handleUiEvent(event);
			}
        });
        
        registerBtn.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View paramView) 
			{

			}
        	
        });
        
		return new AndroidView(fullView);
	}
	
	private AndroidView createUser(int state)
	{
		
        View fullView = View.inflate(context, R.layout.personalinfo, null);
        
		ActionBar actionBar = (ActionBar) fullView.findViewById(R.id.actionbar);
		actionBar.setTitle("用户信息");
        
        return new AndroidView(fullView);
	}
	
	private AndroidView createSetting(int state)
	{
        View fullView = View.inflate(context, R.layout.setting, null);
		
		ActionBar actionBar = (ActionBar) fullView.findViewById(R.id.actionbar);
		actionBar.setTitle("系统设置");
        
		String[] settings = context.getResources().getStringArray(
				R.array.setting_list_title);
		
		ListView listView = (ListView) fullView.findViewById(R.id.listview);
		listView.setAdapter(new SettingAdapter(context, settings));
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 4) {

				} else if (position == 5) {

				}
			}
		});
		
		return new AndroidView(fullView);
	}
	
	class ImagePageAdapter extends PagerAdapter {

		@Override
		public void destroyItem(View v, int position, Object arg2) {
			((ViewPager) v).removeView(pageViews.get(position));
		}

		@Override
		public void finishUpdate(View arg0) {
			// TODO Auto-generated method stub
		}

		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public Object instantiateItem(View v, int position) {
			((ViewPager) v).addView(pageViews.get(position));
			return pageViews.get(position);
		}

		@Override
		public boolean isViewFromObject(View v, Object arg1) {
			// TODO Auto-generated method stub
			return v == arg1;
		}

		@Override
		public void startUpdate(View arg0) {
			// TODO Auto-generated method stub
		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
			// TODO Auto-generated method stub
		}

		@Override
		public Parcelable saveState() {
			// TODO Auto-generated method stub
			return null;
		}
	}
	
	class ImagePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int position) {
			currrentItem = position % pageViews.size();
			pagesText.setText((currrentItem + 1) + "/" + pageViews.size());
			viewPager.setCurrentItem(position);
		}

	}
	
	class ImagePageClickListener implements OnClickListener {
		@Override
		public void onClick(View paramView) {

		}

	}
	
	class SettingAdapter extends BaseAdapter {

		private String[] mitems;

		private LayoutInflater mInflater;

		public SettingAdapter(Context context, String[] items) {
			this.mInflater = LayoutInflater.from(context);
			this.mitems = items;
		}

		@Override
		public int getCount() {
			return mitems.length;
		}

		@Override
		public Object getItem(int position) {
			return mitems[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.yy_setting_list_item,
						null);
				TextView title = (TextView) convertView
						.findViewById(R.id.list_item_title);
				title.setText(mitems[position]);

				if (position <= 3) {
					CheckBox checkbox = (CheckBox) convertView
							.findViewById(R.id.list_item_value);
					checkbox.setVisibility(View.VISIBLE);
				}
				RelativeLayout groupView = (RelativeLayout) convertView
						.findViewById(R.id.group_content);
				if (position == 0 || position == 4) {
					LinearLayout divide_view = (LinearLayout) convertView
							.findViewById(R.id.divide_view);
					divide_view.setVisibility(View.VISIBLE);
					groupView.setBackgroundResource(R.drawable.yy_top_item_bg);
				} else if (position == 3 || position == 5) {
					groupView
							.setBackgroundResource(R.drawable.yy_bottom_item_bg);
				} else {
					groupView
							.setBackgroundResource(R.drawable.yy_middle_item_bg);
				}
			}

			return convertView;
		}

	}
	
}


